package com.lemon.smartsafety.ui.adapter.news;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.CardView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lemon.smartsafety.R;
import com.lemon.smartsafety.app.IApplication;
import com.lemon.smartsafety.entity.news.ArticleInfoVo;
import com.lemon.smartsafety.listener.RecyclerTClickListener;
import com.lemon.smartsafety.utils.common.DateUtils;
import com.lemon.smartsafety.utils.common.StringUtils;

import java.util.List;

import kale.adapter.LemonRcvAdapter;
import kale.adapter.item.AdapterItem;

/**
 * Created by Cooliean on 16/3/29.
 */
public class NewsAdapter extends LemonRcvAdapter<ArticleInfoVo> {
    private RecyclerTClickListener<ArticleInfoVo> mListener;
    public NewsAdapter(@Nullable List<ArticleInfoVo> data) {
        super(data);
    }

    public NewsAdapter(List<ArticleInfoVo> data, RecyclerTClickListener<ArticleInfoVo> listener) {
        super(data);
        this.mListener = listener;
    }

    @NonNull
    @Override
    public AdapterItem createItem(Object type) {
        return new NewsItem();
    }

    public class NewsItem implements AdapterItem<ArticleInfoVo> {

        LinearLayout ripple;
        CardView   mainCardView;
        TextView   tvItemTitle;
        TextView   tvItemSub;


        private ArticleInfoVo mData;
        private int           mPosition;


        @Override
        public int getLayoutResId() {
            return R.layout.item_news_list;
        }

        @Override
        public void bindViews(View root) {
            ripple = (LinearLayout) root.findViewById(R.id.ripple);
            mainCardView = (CardView) root.findViewById(R.id.main_card_view);
            tvItemTitle = (TextView) root.findViewById(R.id.tv_item_title);
            tvItemSub = (TextView) root.findViewById(R.id.tv_item_sub);
        }

        @Override
        public void setViews() {
            ripple.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mListener != null) {
                        mListener.onItemClick(mData, mPosition);
                    }
                }
            });
         /*   ripple.setOnRippleCompleteListener(new RippleView.OnRippleCompleteListener() {
                @Override
                public void onComplete(RippleView rippleView) {
                    if (mListener != null) {
                        mListener.onItemClick(mData, mPosition);
                    }
                }
            });*/
        }

        @Override
        public void handleData(ArticleInfoVo data, int position) {
            mData = data;
            mPosition = position;

            tvItemTitle.setText(StringUtils.nullValue(data.getArticleTitle()));
            String subTitle = String.format(IApplication.context().getString(R.string.news_send_time), DateUtils.getFormatDateStringWithDateString(data.getPublishDate(), DateUtils.dateFormatYMDHM));
            tvItemSub.setText(subTitle);
        }
    }
}
